package com.penghaisoft.microwms.product.web.controller.intowarehouse;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IWmsCommonService;
import com.penghaisoft.microwms.common.wms.model.ProHandIn;
import com.penghaisoft.microwms.common.wms.model.ProOrderInSn;
import com.penghaisoft.microwms.product.service.IProHandInService;
import com.penghaisoft.microwms.product.service.IProOrderInSnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 成品入库管理相关Controller
 * @author luoteng
 * @date 2018年6月6日 下午3:01:43 
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/intowarehouse")
@Slf4j
public class IntoWarehouseController extends BaseController {

	@Autowired
	private IProOrderInSnService proOrderInSnService;
	
	@Autowired
	private IProHandInService proHandInService;
	
	@Autowired
	private IWmsCommonService wmsCommonService;

	/**
	 * 新增记录
	 * 
	 * @param proOrderInSn
	 * @return
	 */
	@PostMapping("proorderin/c")
	public TransResult create(ProOrderInSn proOrderInSn) {
		proOrderInSn.preInsert();
		Resp resp = proOrderInSnService.create(proOrderInSn);
		return fromResp(resp);
	}

	/**
	 * 删除记录（逻辑删除active_flag=0)
	 * 
	 * @param proOrderInSn
	 * @return
	 */
	@PostMapping("proorderin/d")
	public TransResult delete(ProOrderInSn proOrderInSn) {
		proOrderInSn.preUpdate();
		Resp resp = proOrderInSnService.delete(proOrderInSn);
		return fromResp(resp);
	}

	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @return
	 */
	@PostMapping("proorderin/list")
	public TransResult list(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			ProOrderInSn proOrderInSn) {
//		入库状态 0：创建 1：已下线入库
//		proOrderInSn.setBarcodeStatus("1");
		Pager<ProOrderInSn> result = proOrderInSnService.findListByCondition(
				page, rows, proOrderInSn);
		return success(result);
	}
	
	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @return
	 */
	@PostMapping("proorderin/list1")
	public TransResult list1(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			ProOrderInSn proOrderInSn) {
//		入库状态 0：创建 1：已下线入库
//		proOrderInSn.setBarcodeStatus("1");
		Pager<ProOrderInSn> result = proOrderInSnService.findListByCondition1(
				page, rows, proOrderInSn);
		return success(result);
	}

	/**
	 * 查询列表
	 *
	 * @param pager
	 * @return
	 */
	@PostMapping("proorderin/findSummaryList")
	public TransResult findSummaryList(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			ProOrderInSn proOrderInSn) {
		Pager<ProOrderInSn> result = proOrderInSnService.findSummaryList(page, rows, proOrderInSn);
		return success(result);
	}
	/**
	 * 成品入库统计--库管查看
	 * 被报废的条码不算在统计范围中
	 */
	@PostMapping("proorderin/findSummaryListKG")
	public TransResult findSummaryListKG(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			ProOrderInSn proOrderInSn) {
		proOrderInSn.setUserDefined4("!BF");
		Pager<ProOrderInSn> result = proOrderInSnService.findSummaryList(page, rows, proOrderInSn);
		return success(result);
	}
	
	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @return
	 */
	@PostMapping("proorderin/model/list")
	public TransResult listSelect(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			ProOrderInSn proOrderInSn) {
//		入库状态 0：创建 1：已下线入库
//		proOrderInSn.setBarcodeStatus("1");
		Pager<ProOrderInSn> result = proOrderInSnService.findModelList(
				page, rows, proOrderInSn);
		return success(result);
	}

	/**
	 * 查询单条
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("proorderin/r/{id}")
	public TransResult queryById(@PathVariable String id) {
		ProOrderInSn proOrderInSn = proOrderInSnService.findById(id);
		return success(proOrderInSn);
	}

	/**
	 * 修改记录
	 * 
	 * @param proOrderInSn
	 * @return
	 */	
	@PostMapping("proorderin/u")
	public TransResult update(ProOrderInSn proOrderInSn) {
		proOrderInSn.preUpdate();
		Resp resp = proOrderInSnService.update(proOrderInSn);
		return fromResp(resp);
	}

	/**
	 * 查询列表
	 * 
	 * @param pager
	 * @return
	 */
	@PostMapping("prohandin/list")
	public TransResult handInlist(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			ProHandIn proHandIn) {
		proHandIn.setActiveFlag("1");
		Pager<ProHandIn> result = proHandInService.findListByCondition(
				page, rows, proHandIn);
		return success(result);
	}
	
	/** 
	 * @Title proHandInCreate
	 * @Description 成品手工入库批量新增
	 * @author luoteng
	 * @date 2018年6月13日:下午3:27:54
	 * @param proHandIn
	 * @return TransResult
	 */
	@PostMapping("prohandin/batch/insert")
	public TransResult proHandInCreate(@RequestBody List<ProHandIn> list) {
		TransResult transResult = new TransResult();
		if(list == null || list.isEmpty()){
			transResult.setCode(Constant.FAIL);
			transResult.setMsg("请选择成品型号及数量！");
			return transResult;
		}else{
			String handInNo = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_SGR, Constant.SEQ_PREFIX_SHORT_REDIS_PRO_SGR, 1).get(0);
			Resp resp = proHandInService.batchInsert(list, handInNo, getLoginName());
			
			transResult.setCode(resp.getCode());
			transResult.setMsg(resp.getMsg());
		}
		
		return transResult;
	}
	
	/** 
	 * @Title proHandInUpdate
	 * @Description 手工入库单 单条修改
	 * @author luoteng
	 * @date 2018年6月29日:下午12:59:06
	 * @param proHandIn
	 * @return TransResult
	 */
	@PostMapping("prohandin/update")
	public TransResult proHandInUpdate(ProHandIn proHandIn) {
		TransResult transResult = new TransResult();
		transResult.setCode(Constant.SUCCESS);
		ProHandIn ob = new ProHandIn();
		ob.setActiveFlag("1");
		ob.setHandInId(proHandIn.getHandInId());

		List<ProHandIn> list  = proHandInService.queryListByAny(ob);
		if(list != null && !list.isEmpty()){
			ProHandIn tmp = list.get(0);
//			0新建 1入库中 2入库完成
			if(tmp.getHandInStatus() != null && !tmp.getHandInStatus().equals("0")){
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("该手工入库单状态为入库中或者入库完成，不能修改！");
				return transResult;
			}
		}
		
		proHandIn.setLastModifiedBy(getLoginName());
		proHandIn.setGmtModified(new Date());
		proHandInService.update(proHandIn);
		
		return transResult;
	}
	
	/** 
	 * @Title getProHandInList
	 * @Description 根据手工入库单号查询列表
	 * @author luoteng
	 * @date 2018年6月13日:下午4:52:17
	 * @param contractNo
	 * @return TransResult
	 */
	@GetMapping("prohandin/get/{handInNo}")
	public TransResult getProHandInList(@PathVariable("handInNo")String handInNo) {
		TransResult transResult = new TransResult();
		transResult.setCode(Constant.SUCCESS);
		
		ProHandIn ob = new ProHandIn();
		ob.setActiveFlag("1");
		ob.setHandInNo(handInNo);

		List<ProHandIn> list = new ArrayList<ProHandIn>();
		list = proHandInService.queryListByAny(ob);
		if(list != null && !list.isEmpty()){
			for(ProHandIn tmp : list){
//				0新建 1入库中 2入库完成
				if(tmp.getHandInStatus() != null && !tmp.getHandInStatus().equals("0")){
					transResult.setCode(Constant.FAIL);
					transResult.setMsg("该手工入库单状态为入库中或者入库完成，不能修改！");
					return transResult;
				}
			}
			transResult.setData(list);
		}
		
		return transResult;
	}
	
	/** 
	 * @Title getProHandInList
	 * @Description 根据手工入库单号查询列表
	 * @author luoteng
	 * @date 2018年6月13日:下午4:52:17
	 * @param contractNo
	 * @return TransResult
	 */
	@GetMapping("prohandin/get/id/{handInId}")
	public TransResult getProHandInIdList(@PathVariable("handInId")String handInId) {
		TransResult transResult = new TransResult();
		transResult.setCode(Constant.SUCCESS);
		
		ProHandIn ob = new ProHandIn();
		ob.setActiveFlag("1");
		ob.setHandInId(Integer.parseInt(handInId));

		List<ProHandIn> list  = proHandInService.queryListByAny(ob);
		if(list != null && !list.isEmpty()){
			ProHandIn tmp = list.get(0);
//			0新建 1入库中 2入库完成
			if(tmp.getHandInStatus() != null && !tmp.getHandInStatus().equals("0")){
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("该手工入库单状态为入库中或者入库完成，不能修改！");
				return transResult;
			}
			transResult.setData(tmp);
		}
		
		return transResult;
	}
	/**
	 * @Title handInlist
	 * @Description 成品生产统计
	 * @author yanggm
	 */
	@PostMapping("prohandin/production")
	public TransResult handInlist(
			@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows,
			ProOrderInSn proOrderInSn) {
//		proOrderInSn.setActiveFlag("1");
		Pager<ProOrderInSn> result = proOrderInSnService.getProduction(
				page, rows, proOrderInSn);
		return success(result);
	}
}
